﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityManagementSystem.DAL;
using iTextSharp.text;

namespace UniversityManagementSystem.UI
{
    public partial class StudentEnrollCourse : System.Web.UI.Page
    {
        List<Course> allCourse = new List<Course>();
        List<Course> enrolledCourses = new List<Course>();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                messageLabel.Text = "";
                nameTextBox.Text = "";
                dateTextBox.Text = "";
                emailTextBox.Text = "";
                deptTextBox.Text = "";
                List<Course> courseToEnroll = new List<Course>();
                dateTextBox.Text = DateTime.Now.ToString("dd/MM/yyyy");
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Error occured during loading page";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void showAllButton_Click(object sender, EventArgs e)
        {
            try
            {
                Course course = new Course();
                List<Course> allCourses = new List<Course>();
                allCourses = course.GetAllCourses();
                allCoursesGridView.DataSource = allCourses;
                allCoursesGridView.DataBind();
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Unable to load courses.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
           
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            Student aStudent = new Student();
            Course allCourses = new Course();
            try
            {
                aStudent = aStudent.SearchAstudent(regNoTextBox.Text);
                if (aStudent.name == null)
                {
                    messageLabel.Text = "No student found";
                    messageLabel.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    nameTextBox.Text = aStudent.name;
                    emailTextBox.Text = aStudent.email;
                    deptTextBox.Text = aStudent.departmentName;
                    allCourse = allCourses.GetAllCoursesByDepartment(deptTextBox.Text);
                    ViewState["enrollCourse"] = allCourse;
                    ViewState["aStudent"] = aStudent;
                    enrolledCourses = allCourses.GetAllEnrolledCourses(aStudent.RegistrationNo);
                    ViewState["enrolledCourse"] = enrolledCourses;
                    courseDropDownList.DataSource = allCourse;
                    courseDropDownList.DataValueField = "CourseName";
                    courseDropDownList.DataBind();
                    enrolledCoursesGridView.DataSource = enrolledCourses;
                    enrolledCoursesGridView.DataBind();
                }

                
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Error occured" + exception;
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void courseDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void enrollButton_Click(object sender, EventArgs e)
        {
            string enrollMessage = ""; 
            try
            {
                Student aStudent = new Student();
                Course aCourse = new Course();
                List<Course> allCourses = new List<Course>();
                List<Course> enrolled = new List<Course>();
                aStudent = (Student) ViewState["aStudent"];
                allCourses = (List<Course>)ViewState["enrollCourse"];
                aCourse = allCourses.Find(delegate(Course FindaCourse) { return FindaCourse.CourseName == courseDropDownList.SelectedValue; });
                enrollMessage = aStudent.EnrollAcourse(aStudent.RegistrationNo, aCourse);
                enrolled = (List<Course>) ViewState["enrolledCourse"];
                enrolled.Add(aCourse);
                enrolledCoursesGridView.DataSource = enrolled;
                enrolledCoursesGridView.DataBind();
                messageLabel.Text = enrollMessage;
                messageLabel.ForeColor = System.Drawing.Color.Blue;
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Unable to enroll Course";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }

        }
    }
}